﻿#region

using System.Collections.Generic;
using EsbMonitorExtension.Infrastructure;
using EsbMonitorExtension.MonitorTasks;
using log4net;
using NServiceBus;
using NServiceBus.Unicast;

#endregion

namespace EsbMonitorExtension
{
    public class InitializeEsbMonitor : IWantToRunAtStartup
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof (InitializeEsbMonitor));
        public IBus Bus { get; set; }

        public IMonitorTasksBuilder MonitorTasksBuilder { get; set; }

        #region IWantToRunAtStartup Members

        public InitializeEsbMonitor()
        {
            MonitorTasksBuilder = new MonitorTasksBuilderImpl();
        }

        public void Run()
        {
            Logger.Info("Initializing Esb Monitoring...");

            IUnicastBus unicastBus = (IUnicastBus) Bus;

            List<IMonitorTask> tasks = new List<IMonitorTask>
                                           {
                                               MonitorTasksBuilder.BuildServiceStartedTask(unicastBus),
                                               MonitorTasksBuilder.BuildPublisherStartedTask(unicastBus),
                                               MonitorTasksBuilder.BuildHandlerStartedTask(unicastBus),
                                               MonitorTasksBuilder.BuildMessageAssembliesLoadedTask(unicastBus),
                                               MonitorTasksBuilder.BuildInitServiceAliveServiceTask(unicastBus)
                                           };

            tasks.ForEach(t => t.RunTask());
        }

        public void Stop()
        {
        }

        #endregion
    }
}